﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using FirstFloor.ModernUI.Presentation;
using FirstFloor.ModernUI.Windows.Controls;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using Tisbi.LawFirm.BL;
using Tisbi.LawFirm.BL.Services;

namespace Tisbi.LawFirm.UI.ViewModel
{
    public abstract class BaseCRUDItemViewModel<T> : ViewModelBase, IDataErrorInfo
    {
         // Default ctor
        protected BaseCRUDItemViewModel(T model)
        {
            Model = model;
        }

        private T _model;
        public T Model
        {
            get { return _model; }
            set
            {
                _model = value;
                RaisePropertyChanged(() => Model);
            }
        }

        public void UpdateListData<TColType>(ref ObservableCollection<TColType> collection)
        {
            ObservableCollection<TColType> localCollection = null;

            SimpleIoc.Default.GetInstance<IServiceAgentBase<TColType>>().GetItem
                (
                    (entities, error) =>
                    {
                        if (error == null)
                        {
                            localCollection = new ObservableCollection<TColType>(entities);
                        }
                            // Otherwise notify view that there's an error
                        else
                        {
                            Notifyer.NotifyError("Unable to retrieve items", error);
                        }
                    }
                );

            if (localCollection != null)
            {
                collection = localCollection;
            }
        }

        public ObservableCollection<TColType> CreateNewDependencyItemDialog<TColType, TControl>(string dialogTitle, out TColType selectedItem) where TControl : UserControl, new() where TColType : class
        {
            selectedItem = null;
            var control = new TControl();
            var popUpDialog = new ModernDialog
            {
                Title = dialogTitle,
                Content = control,
                MinWidth = 900,
                MinHeight = 760
            };

            var observableCollection = new ObservableCollection<TColType>();
            TColType localSselectedItem = null;

            popUpDialog.Buttons = new[] { popUpDialog.OkButton };
            popUpDialog.OkButton.Command = new RelayCommand(o =>
            {
                var element = (control.Content as FrameworkElement);
                if (element != null)
                {
                    var viewModel = element.DataContext as BaseViewModel<TColType, IServiceAgentBase<TColType>>;
                    if (viewModel != null)
                    {
                        localSselectedItem = viewModel.SelectedModelItem;
                    }
                }

                UpdateListData(ref observableCollection);
                popUpDialog.Close();
            });

            popUpDialog.ShowDialog();

            selectedItem = localSselectedItem ?? observableCollection.FirstOrDefault();
            return observableCollection;
        }

        public abstract string Error { get; }

        public abstract string this[string columnName] { get; }
    }
}
